package com.zjut.an.util.page;

import java.util.List;
import java.util.Vector;

public class Pagination {

	private int sumPageNumber;

	private int sumItemNumber;

	private int pageNumber;

	private int showItemNumber;

	private String visitPageURL;

	private String pageHTML;

	public List interceptListByStarItemNumber(List itemList) {
		int startItem = (pageNumber - 1) * showItemNumber;
		List tpItemList = new Vector();
		if (itemList == null) {
			return null;
		}
		sumItemNumber = itemList.size();
		if (sumItemNumber % showItemNumber == 0) {
			sumPageNumber = sumItemNumber / showItemNumber;
		} else {
			sumPageNumber = sumItemNumber / showItemNumber + 1;
		}

		for (int i = 0; i < itemList.size(); i++) {
			// 不要超过截取的最大条数,
			if ((i) == (startItem + showItemNumber)) {
				break;
			}
			// 当循环到开始条数的时候，将对象添加到新的列表里面。
			if ((i) >= startItem) {
				tpItemList.add(itemList.get(i));
			}
		}

		return tpItemList;
	}

	public String buildHTML(String tWidth) {
		
		String tableWidth = tWidth;
		String visitFirstPageURL = "";
		String visitNextPageURL = "";
		String visitPreviousURL = "";
		String pagelist = "";
		String visitEndURL = "";
		visitPageURL = visitPageURL + "showItemNumber=" + showItemNumber
				+ "&pageNumber=";
		//System.out.println("######################"+pageNumber);
		//System.out.println("######################"+sumPageNumber);
		if (pageNumber == 1) {
			//visitFirstPageURL = "<li>[首页]</li>";
			visitPreviousURL = "<li class='paginItem'><a href='#'><span class='pagepre'></span></a></li>";
		} else {
			//visitFirstPageURL = "<a href='" + visitPageURL+ "1'>[首页]</a>&nbsp; ";
			visitPreviousURL = "<li class='paginItem'><A href='" + visitPageURL + (pageNumber - 1)
					+ "'><span class='pagepre'></span></A></li>";
		}
		if (pageNumber == sumPageNumber || sumPageNumber==0 ) {
			visitNextPageURL = "<li class='paginItem'><a href='#'><span class='pagenxt'></span></a></li>";
			//visitEndURL = "[尾页]&nbsp;";
		} else {
			visitNextPageURL = "<li class='paginItem'><a href='" + visitPageURL + (pageNumber + 1)
					+ "'><span class='pagenxt'></span></a></li>";
			//visitEndURL = "<a href='" + visitPageURL + sumPageNumber + "'>[尾页]</a>";
		}
		
		if (pageNumber < 10) {
			int p1 = 1;
			int p2 = sumPageNumber>11?11:sumPageNumber;
			for(int i = p1; i<= p2; i++) {
				pagelist+="<li";
				if (i==pageNumber) {
					pagelist+=" class='paginItem current'";
				} else {
					pagelist+=" class='paginItem'";
				}
				pagelist+="><a href='"+visitPageURL+ i + "'>"+i+"</a></li>";
			}
		} else {
			int p1 = (pageNumber/10)*10-2;
			int p2 = (pageNumber/10)*10+8;
			if (sumPageNumber<p2) {
				p2 = sumPageNumber;
			}
			for(int i = p1; i<= p2; i++) {
				pagelist+="<li";
				if (i==pageNumber) {
					pagelist+=" class='paginItem current'";
				} else {
					pagelist+=" class='paginItem'";
				}
				pagelist+="><a href='"+visitPageURL+ i + "'>"+i+"</a></li>";
			}
		}
		

		StringBuffer sb = new StringBuffer();
		//sb.append("当前是第[" + pageNumber + "]页&nbsp;");
		//sb.append("共[" + sumPageNumber + "]页&nbsp;" + "共[" + sumItemNumber + "]条&nbsp;");
		visitFirstPageURL="<div class='message'>共<i class='blue'>"+sumItemNumber+"</i>条记录，当前显示第&nbsp;<i class='blue'>"+pageNumber+"&nbsp;</i>页</div>";
		
		sb.append(visitFirstPageURL);		
		sb.append("<ul class='paginList'>");
		sb.append(visitPreviousURL);
		sb.append(pagelist);
		sb.append(visitNextPageURL);
		
		sb.append("</ul>");
		
		sb.append(visitEndURL);
		return sb.toString();
	}
	
	public String buildHTMLSimple(String tWidth) {
		
		String tableWidth = tWidth;
		String visitFirstPageURL = "";
		String visitNextPageURL = "";
		String visitPreviousURL = "";
		String pagelist = "";
		String visitEndURL = "";
		visitPageURL = visitPageURL + "showItemNumber=" + showItemNumber
				+ "&pageNumber=";
		if (pageNumber == 1) {
			//visitFirstPageURL = "<li>[首页]</li>";
			visitPreviousURL = "<li><a href='#'>上一页</a></li>";
		} else {
			//visitFirstPageURL = "<a href='" + visitPageURL+ "1'>[首页]</a>&nbsp; ";
			visitPreviousURL = "<li><A href='" + visitPageURL + (pageNumber - 1)
					+ "'>上一页</A></li>";
		}
		if (pageNumber == sumPageNumber  || sumPageNumber==0 ) {
			visitNextPageURL = "<li><a href='#'>下一页</a></li>";
			//visitEndURL = "[尾页]&nbsp;";
		} else {
			visitNextPageURL = "<li><a href='" + visitPageURL + (pageNumber + 1)
					+ "'>下一页</a></li>";
			//visitEndURL = "<a href='" + visitPageURL + sumPageNumber + "'>[尾页]</a>";
		}
		
		StringBuffer sb = new StringBuffer();
		//sb.append("当前是第[" + pageNumber + "]页&nbsp;");
		//sb.append("共[" + sumPageNumber + "]页&nbsp;" + "共[" + sumItemNumber + "]条&nbsp;");
		
		sb.append("<ul pager float-right>");
		sb.append(visitFirstPageURL);
		sb.append(visitPreviousURL);
		
		sb.append(visitNextPageURL);
		sb.append(visitEndURL);
		sb.append("</ul>");
		return sb.toString();
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public int getShowItemNumber() {
		return showItemNumber;
	}

	public void setShowItemNumber(int showItemNumber) {
		this.showItemNumber = showItemNumber;
	}

	public int getSumItemNumber() {
		return sumItemNumber;
	}

	public void  setSumItemNumber(int sumItemNumber) {
		 this.sumItemNumber=sumItemNumber;
	}
	
	public int getSumPageNumber() {
		return sumPageNumber;
	}
	
	public void  setSumPageNumber(int sumPageNumber ) {
		this.sumPageNumber=sumPageNumber;
	}
	
	public String getVisitPageURL() {
		return visitPageURL;
	}

	public void setVisitPageURL(String visitPageURL) {
		this.visitPageURL = visitPageURL;
	}

	public String getPageHTML() {
		return pageHTML;
	}
}
